package org.jsystem.swttestingclient;

import java.net.Socket;
import java.util.Vector;

import jsystem.framework.system.SystemObjectImpl;

import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;

import com.gerhardtinformatics.rcprobot.IRCPRobot;

public class SwtTestingClient extends SystemObjectImpl implements IRCPRobot{
	
	protected String host = "127.0.0.1";
	protected int port = 8099;
	protected static String handlerName = "swtAgent";
	
	public void init() throws Exception{
		super.init();
	}
	
	
	
	
	public boolean isLaunched(){
		try {
			Socket s = new Socket(host, getPort());
			s.close();
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	private synchronized Object handleXmlCommand(String title, String command, Object... objects) {
		Vector<Object> v = new Vector<Object>();
		for (Object object : objects){
			if (object == null){
				v.add("");
			}else{
				v.addElement(object);
			}
		}
		return handleXmlCommand(title, command, v);
	}

	/**
	 * transfer xml request to server
	 * 
	 */
	private synchronized Object handleXmlCommand(String title, String command, Vector<Object> v) {
		Object o = null;
		report.report(title);
		try {
			o = execute(command, v);
		} catch (Exception e) {
			throw new RuntimeException(e);

		}

		if (o instanceof XmlRpcException) {
			// server exception

			throw new RuntimeException((XmlRpcException) o);
		}

		return o;
	}

	/**
	 * creates an XmlRpcClient and calls it's execute method to connect
	 * to a server
	 * @param command
	 * @param params
	 * @return
	 * @throws Exception
	 */
	protected Object execute(String command, Vector<Object> params) throws Exception {
		XmlRpcClient client = new XmlRpcClient("http://" + host + ":" + port + "/RPC2");
		return client.execute(command, params);
	}
	protected Object execute(String title, String methodName, Object...params){
		return handleXmlCommand(title, handlerName + "." + methodName, params);
	}

	
	
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}




	@Override
	public void assertActivePerspective(String name){
		execute("Assert active perspective: " + name, "assertActivePerspective", name);
	}




	@Override
	public void assertMenuItemChecked(String path) {
		execute("Assert menu item checked: " + path, "assertMenuItemChecked", path);
	}




	@Override
	public void assertMenuItemDisabled(String path) {
		execute("Assert menu item checked: " + path, "assertMenuItemChecked", path);
	}




	@Override
	public void assertMenuItemEnabled(String path) {
		execute("Assert menu item checked: " + path, "assertMenuItemChecked", path);
	}




	@Override
	public void assertMenuItemExists(String path) {
		execute("Assert menu item checked: " + path, "assertMenuItemChecked", path);
	}




	@Override
	public void assertMenuItemNotChecked(String path) {
		execute("Assert menu item not checked: " + path, "assertMenuItemNotChecked", path);
	}




	@Override
	public void assertNodeExistsInView(String viewName, String treePath) {
		execute("Assert node: " + treePath + " exist in view: " + viewName, "assertNodeExistsInView", viewName, treePath);
	}




	@Override
	public void assertNodeNotExistsInView(String viewName, String treePath) {
		execute("Assert node: " + treePath + " dosn't exist in view: " + viewName, "assertNodeNotExistsInView", viewName, treePath);
	}




	@Override
	public void assertToolBarActionDisabled(String toolTipText) {
		execute("Assert Tool-bar action disable: " + toolTipText, "assertToolBarActionDisabled", toolTipText);
	}




	@Override
	public void assertToolBarActionEnabled(String toolTipText) {
		execute("Assert Tool-bar action enable: " + toolTipText, "assertToolBarActionEnabled", toolTipText);
	}




	@Override
	public void assertToolBarActionExists(String toolTipText) {
		execute("Assert Tool-bar action exist: " + toolTipText, "assertToolBarActionExists", toolTipText);
	}




	@Override
	public void assertToolBarActionNotExists(String toolTipText) {
		execute("Assert Tool-bar action not exist: " + toolTipText, "assertToolBarActionNotExists", toolTipText);
	}




	@Override
	public void assertToolBarDropDownActionDisabled(String text) {
		execute("Assert Tool-bar drop down action disabled: " + text, "assertToolBarDropDownActionDisabled", text);
	}




	@Override
	public void assertToolBarDropDownActionEnabled(String text) {
		execute("Assert Tool-bar drop down action enabled: " + text, "assertToolBarDropDownActionEnabled", text);
	}




	@Override
	public void assertToolBarDropDownActionExists(String text) {
		execute("Assert Tool-bar drop down action exist: " + text, "assertToolBarDropDownActionExists", text);
	}




	@Override
	public void assertToolBarDropDownActionNotExists(String text) {
		execute("Assert Tool-bar drop down action not exist: " + text, "assertToolBarDropDownActionNotExists", text);
	}




	@Override
	public void assertViewExists(String viewName) {
		execute("Assert view exists: " + viewName, "assertViewExists", viewName);
	}




	@Override
	public void assertViewNotExists(String viewName) {
		execute("Assert view not exists: " + viewName, "assertViewNotExists", viewName);
	}




	@Override
	public void assertWindowTitle(String title) {
		execute("Assert window title: " + title, "assertWindowTitle", title);
	}




	@Override
	public void clickButton(String buttonText) {
		execute("Click button: " + buttonText, "clickButton", buttonText);
	}




	@Override
	public void clickButtonInEditor(String button, String path) {
		execute("Click button: " + button + " in editor " + path, "clickButtonInEditor  ", button, path);
	}




	@Override
	public void clickLabel(String labelText) {
		execute("Click label: " + labelText, "clickLabel", labelText);
	}




	@Override
	public void clickPopUpTree(String viewName, String treeItemPath,
			String menuPath) {
		execute("Click popup tree: " + treeItemPath, "clickPopUpTree", viewName, treeItemPath, menuPath);
	}




	@Override
	public void clickTab(String tabText) {
		execute("Click tab: " + tabText, "clickTab", tabText);
	}




	@Override
	public void closeView(String viewName) {
		execute("Close view: " + viewName, "closeView", viewName);
	}




	@Override
	public void closeWelcomeView() {
		execute("Close welcome view", "closeWelcomeView");
	}




	@Override
	public void delay(int ms) {
		execute("Sleep " +(ms / 1000) + " sec.", "delay", ms);
	}




	@Override
	public void doubleClickTree(String viewName, String path) {
		execute("Double click tree " + path + ", in view " + viewName, "doubleClickTree", viewName, path);
	}




	@Override
	public void enterCharacter(char c) {
		execute("Enter character " + c, "enterCharacter", (int)c);
	}




	@Override
	public void enterCharacters(String string) {
		execute("Enter characters " + string, "enterCharacters", string);
	}




	@Override
	public void enterKeyCombination(String accelerator) {
		execute("Enter key combination " + accelerator, "enterKeyCombination", accelerator);
	}




	@Override
	public void enterSpecialKey(String accelerator) {
		execute("Enter key combination " + accelerator, "enterSpecialKey", accelerator);
	}




	@Override
	public void enterSpecialKey(String accelerator, int times) {
		execute("Enter special key " + accelerator +"," + times + " times ", "enterSpecialKey", accelerator, times);
	}




	@Override
	public void enterString(String string) {
		execute("Enter string " + string, "enterString", string);
	}




	@Override
	public void holdKey(String accelerator) {
		execute("Hold key " + accelerator, "holdKey", accelerator);
	}




	@Override
	public void openPerspective(String name) {
		execute("Open perspective " + name, "openPerspective", name);
	}




	@Override
	public void openView(String path) {
		execute("Open view " + path, "openView", path);
	}




	@Override
	public void releaseKey(String accelerator) {
		execute("Release key " + accelerator, "releaseKey", accelerator);
	}




	@Override
	public void selectContextMenuInView(String viewName, String path) {
		execute("Select context menu " + path +", in view " + viewName, "selectContextMenuInView", viewName, path);
	}




	@Override
	public void selectMenuItem(String path) {
		execute("Select menu item " + path, "selectMenuItem", path);
	}




	@Override
	public void selectNodeContextMenuInView(String viewName, String treePath,
			String menuPath) {
		execute("Select node context menu " + menuPath +", in view " + viewName, "selectNodeContextMenuInView", viewName, treePath, menuPath);
	}




	@Override
	public void selectNodeInView(String viewName, String path) {
		execute("Select node " + path +", in view " + viewName, "selectNodeInView", viewName, path);
	}




	@Override
	public void selectPageInEditor(String path) {
		execute("Select page " + path +" in editor", "selectPageInEditor", path);
	}




	@Override
	public void selectToolBarAction(String toolTipText) {
		execute("Select tool-bar action " + toolTipText , "selectToolBarAction", toolTipText);
	}




	@Override
	public void selectToolBarDropDownAction(String text) {
		execute("Select tool-bar drop down action " + text , "selectToolBarDropDownAction", text);
	}




	@Override
	public void setFocusOnEditor(String editorName) {
		execute("Select focus on editor " + editorName , "setFocusOnEditor", editorName);
	}




	@Override
	public void setFocusOnView(String viewName) {
		execute("Select focus on view " + viewName , "setFocusOnView", viewName);
	}




	@Override
	public void waitForShellShowing(String title) {
		execute("Wait for shell showing" + title , "waitForShellShowing", title);
	}




	@Override
	public void waitForShellShowing(String title, long timeout) {
		execute("Wait for shell showing" + title , "waitForShellShowing", title, (int)timeout);
	}

}
